package com.itboye.jinpuyiyuan.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.alibaba.mobileim.channel.event.IWxCallback;
import com.alibaba.mobileim.channel.util.YWLog;
import com.alibaba.mobileim.utility.IMNotificationUtils;
import com.itboye.jinpuyiyuan.MainActivity;
import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.login.LoginActivity;
import com.itboye.jinpuyiyuan.bean.PersonBean;
import com.itboye.jinpuyiyuan.bean.WXBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.IMKit.LoginSampleHelper;
import com.itboye.jinpuyiyuan.utils.IMKit.NotificationInitSampleHelper;
import com.itboye.jinpuyiyuan.utils.IMKit.UserProfileSampleHelper;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.FilterUtil;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;

import java.util.Observable;
import java.util.Observer;

import static com.itboye.jinpuyiyuan.activity.home.finddoctor.DoctorDetailActivity.TAG;


/**
 * Created by Administrator on 2018/1/19 0019.
 */

public class WXEntryActivity extends Activity implements IWXAPIEventHandler, Observer {
    private IWXAPI api;
    private BranchAddressPresenter presenter;
    public  LoginSampleHelper loginSampleHelper;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_illessinfo);
         regToWx();
        //init SDK 添加消息接受监听
        loginSampleHelper = LoginSampleHelper.getInstance();
         presenter=new BranchAddressPresenter(this);
        boolean b = api.handleIntent(getIntent(), this);
        MAlert.alert(b+"");

    }


    @Override
    public void onReq(com.tencent.mm.opensdk.modelbase.BaseReq baseReq) {

        MAlert.alert("fasongchuqule");
    }

    @Override
    public void onResp(com.tencent.mm.opensdk.modelbase.BaseResp baseResp) {

        String code = ((SendAuth.Resp) baseResp).code;
        presenter.loginByWX("","xxxxxx",code);
        Log.d("code===",code);

//        api.handleIntent(getIntent(),this);
//        MAlert.alert("huilaile");
    }
    //微信介入
    private  void  regToWx(){
        api= WXAPIFactory.createWXAPI(this,Const.APP_ID_WX,true);
        api.registerApp(Const.APP_ID_WX);
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity= (ResultEntity) arg;
        if (resultEntity!=null){

            if (Integer.parseInt(resultEntity.getCode())==-2){
                MAlert.alert(resultEntity.getMsg());
                PersonBean wxBean= (PersonBean) resultEntity.getData();
                Intent intent = new Intent(WXEntryActivity.this, LoginActivity.class);
                intent.putExtra("wxapp_openid",wxBean.getWxapp_openid());
                intent.putExtra("wx_unionid",wxBean.getWx_unionid());
                intent.putExtra("wx_nickname",wxBean.getNickname());
                intent.putExtra("wx_head",wxBean.getHead());
                startActivity(intent);
                WXEntryActivity.this.finish();
            }else if(Integer.parseInt(resultEntity.getCode())==0) {
                final PersonBean personBean= (PersonBean) resultEntity.getData();
                Log.d("login==",personBean.getAlibaichuan_id()+"=="+personBean.getPassword()
                        +"uid="+personBean.getUid());
                FilterUtil.putSharePre(WXEntryActivity.this.getApplication(),personBean);
                init(personBean.getAlibaichuan_id(), Const.OPEN_APPKEY);

                loginSampleHelper.login_Sample(personBean.getAlibaichuan_id(), personBean.getPassword(), Const.OPEN_APPKEY, new IWxCallback() {
                    @Override
                    public void onSuccess(Object... objects) {
                        Log.d("login====","success");
                        MAlert.alert("注册ALI_SDK成功");
                        SPUtils.put(WXEntryActivity.this, null,
                                Const.PASSWORD, personBean.getPassword());
                        SPUtils.put(WXEntryActivity.this,null,
                                Const.ALIBAICHUAN,personBean.getAlibaichuan_id());
                    }

                    @Override
                    public void onError(int i, String s) {
                        Log.d("login====","false");
                        Log.d("alert==",i+"=="+s);
                        YWLog.w(TAG, "登录失败，错误码：" + i + "  错误信息：" + s);
                        IMNotificationUtils.getInstance().showToast(WXEntryActivity.this, s);
                    }

                    @Override
                    public void onProgress(int i) {
                        Log.d("login====","onProgress");

                        Log.d("login====","success");
                        MAlert.alert("注册ALI_SDK成功");
                        SPUtils.put(WXEntryActivity.this, null,
                                Const.PASSWORD, personBean.getPassword());
                        SPUtils.put(WXEntryActivity.this,null,
                                Const.ALIBAICHUAN,personBean.getAlibaichuan_id());
                    }
                });

                startActivity(new Intent(WXEntryActivity.this,MainActivity.class));
                Log.d("role",personBean.getRoles().get(0)+"");
                WXEntryActivity.this.finish();

            }else {
                MAlert.alert("获取微信登录信息失败");
                WXEntryActivity.this.finish();
            }
        }
    }


    private static void init(String userId, String appKey){
        //初始化imkit
        LoginSampleHelper.getInstance().initIMKit(userId, appKey);
        //自定义头像和昵称回调初始化(如果不需要自定义头像和昵称，则可以省去)
        UserProfileSampleHelper.initProfileCallback();
        //通知栏相关的初始化
        NotificationInitSampleHelper.init();

    }
}
